/*
 * Copyright (c) 2013, Klaus Hauschild
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
 * that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, this list of conditions and
 *     the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *     and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package de.hauschild.utilsn.core.gamestate;

import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import de.hauschild.utilsn.core.Entity;

/**
 * Controls the provided game states.
 * 
 * @since 0.1.0
 * 
 * @author Klaus Hauschild
 */
public class GameStateController implements Entity {

  private final Map<Class<? extends GameState>, GameState> gameStates = Maps.newHashMap();
  private final Set<Class<? extends GameState>> initializedGameStateTypes = Sets.newHashSet();
  private Class<? extends GameState> currentGameStateType;

  /**
   * Load all given game state types and enters the first one.
   * 
   * @param gameStateTypes
   *          the game state types
   */
  public GameStateController(final Class<? extends GameState> gameStateType,
      final Class<? extends GameState>... gameStateTypes) {
    gameStates.put(gameStateType, load(gameStateType));
    for (final Class<? extends GameState> type : gameStateTypes) {
      gameStates.put(type, load(type));
    }
    nextGameState(gameStateType);
  }

  /**
   * Performs the transition between the current and the next game state. The following steps will be performed:
   * <ul>
   * <li>exit the current game state</li>
   * <li>make the next game state to the current one</li>
   * <li>maybe initialize the new current game state</li>
   * <li>enter the new current game state with the given data</li>
   * </ul>
   * 
   * @param nextGameStateType
   *          the type of the next game state
   * @param data
   *          the data from the previous game state
   */
  public void nextGameState(final Class<? extends GameState> nextGameStateType, final Object... data) {
    final GameState currentGameState = getCurrentGameState();
    if (currentGameState != null) {
      currentGameState.exit();
    }
    final Class<? extends GameState> previousGameStateType = currentGameStateType;
    currentGameStateType = nextGameStateType;
    getCurrentGameState().enter(previousGameStateType, data);
  }

  @Override
  public void paint(final float alpha) {
    getCurrentGameState().paint(alpha);
  }

  @Override
  public void update(final int delta) {
    getCurrentGameState().update(delta);
  }

  /**
   * Gets the current game state. If it is the first request on this game state type it will be initialized.
   * 
   * @return the current game state
   */
  private GameState getCurrentGameState() {
    if (currentGameStateType == null) {
      return null;
    }
    final GameState gameState = gameStates.get(currentGameStateType);
    if (!initializedGameStateTypes.contains(currentGameStateType)) {
      gameState.init();
      initializedGameStateTypes.add(currentGameStateType);
    }
    return gameState;
  }

  private GameState load(final Class<? extends GameState> gameStateType) {
    try {
      final Constructor<? extends GameState> constructor = gameStateType.getConstructor(GameStateController.class);
      return constructor.newInstance(this);
    } catch (final Exception exception) {
      throw new RuntimeException(String.format("Unable to load game state type [%s].", gameStateType), exception);
    }
  }

}
